package org.mybatis.movimenti;

import it.widgets.client.beans.Movimento;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.mybatis.mappers.MovimentiMapper;

@Aspect
public class BusinessLogicMovimentiAsp {
	
	Logger logger = Logger.getLogger( this.getClass().getSimpleName() );
	
	@Before("execution(public * org.mybatis.movimenti.*.*(..))")
	public void beforeMethod(JoinPoint jp) {		
		logger.log(Level.INFO, "#S# before " + jp.getSignature().getName());
		BusinessLogicMovimenti bs = (BusinessLogicMovimenti) jp.getTarget();
		if ( bs != null ){
			bs.openSession();
			bs.setMapper(MovimentiMapper.class);
		}
	    logger.log(Level.INFO, "#E# before " + jp.getSignature().getName());
	}
	
	@After("execution(public * org.mybatis.movimenti.*.*(..))")
	public void afterMethod(JoinPoint jp) {
		logger.log(Level.INFO, "#S# After " + jp.getSignature().getName());
		BusinessLogicMovimenti bs = (BusinessLogicMovimenti) jp.getTarget();
		if ( bs != null ){
			bs.commitSession();
			bs.closeSession();
		}
	    logger.log(Level.INFO, "#E# After " + jp.getSignature().getName());
	}
	
  @AfterReturning(
            pointcut = "execution(public * org.mybatis.movimenti.*.selectAll())",
            returning = "m")
    public void afterReturning(JoinPoint jp, List<Movimento> m){
		logger.log(Level.INFO, "#S# AfterReturning " + jp.getSignature().getName());
		for (Movimento movimento : m) {
			logger.log(Level.INFO, "#######################################");
			logger.log(Level.INFO,  movimento.toString() );	
			logger.log(Level.INFO, "#######################################");			
		}
	    logger.log(Level.INFO, "#E# AfterReturning " + jp.getSignature().getName());
    } 

}
